package cn.itcast.item.config;

import cn.itcast.hmall.config.MqConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqConfig {

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(MqConstants.ITEM_TOPIC_EXCHANGE);
    }
    @Bean
    public Queue upQueue(){
        return new Queue(MqConstants.ITEM_UP_QUEUE);
    }
    @Bean
    public Queue downQueue(){
        return new Queue(MqConstants.ITEM_DOWN_QUEUE);
    }
    @Bean
    public Binding upBinding(@Autowired Queue upQueue, @Autowired TopicExchange topicExchange){
        return BindingBuilder.bind(upQueue).to(topicExchange).with(MqConstants.ITEM_UP_ROUTING);
    }
    @Bean
    public Binding downBinding(@Autowired Queue downQueue, @Autowired TopicExchange topicExchange){
        return BindingBuilder.bind(downQueue).to(topicExchange).with(MqConstants.ITEM_DOWN_ROUTING);
    }

   @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}
